<template>
  <div :class="{ show: show }" class="header-search relative">
    <span
      class="color-active-light pointer size16 "
      v-if="!show"
      @click.stop="click"
      >更多单位>></span
    >
    <svg-icon
      class-name="search-icon size20"
      v-if="!show"
      icon-class="search "
      @click.stop="click"
    />
    <i
      class="el-icon-circle-close size20 bold pointer "
      v-else
      @click.stop="click"
    />
    <el-input
      ref="headerSearchSelect"
      v-model="search"
      remote
      placeholder="请输入单位名称"
      class="header-search-select"
    >
    </el-input>
    <span
      class="absolute pdbtn  color-white pointer"
      :class="isTuihui ? 'active-bg' : 'info-bg'"
      v-show="show && type"
      @click="isTuihui = !isTuihui"
      style="bottom:-500px;z-index:999;left:-138px"
      >退回列表</span
    >
    <div
      class="absolute scrollbar-slinder "
      :class="show ? 'pdlr15 pdtb15 hover-border-active white-bg ' : ''"
      style="bottom:-500px;height:500px;z-index:999;left:-40px"
      :style="{ width: show ? '250px' : '0px' }"
    >
      <div class="relative">
        <el-collapse-transition>
          <div v-show="show">
            <div class="transition-box ">
              <el-popover
                placement="left"
                v-for="(item, index) in controlCompanyList"
                :key="index"
                trigger="hover"
              >
                <div v-if="false">
                  <div class="text-right color-info">
                    <span>
                      <i class="size18 pdl5 el-icon-success color-success"></i
                      >已填报
                    </span>
                    <span>
                      <i class="size18 pdl5 el-icon-error color-danger"></i
                      >未填报
                    </span>
                  </div>
                  <div
                    class=" size16 lh24 flex align-center justify-between "
                    v-for="(table, tableIndx) in item.xqTables"
                    :key="tableIndx"
                  >
                    {{ table.title }}
                    <i
                      class="size18 pdl5"
                      :class="
                        table.count > 0
                          ? 'el-icon-success color-success'
                          : 'el-icon-error color-danger'
                      "
                    ></i>
                  </div>
                </div>
                <div
                  slot="reference"
                  :class="nowCompanyId == item.companyId ? 'menuHover-bg' : ''"
                  @click="setCompanyId(item.companyId)"
                  class="size16 pointer mr8 pd5 hover-border mrb5"
                  :style="{
                    color: getbgColor(item.color)
                  }"
                >
                  {{ item.companyName }}
                </div>
              </el-popover>
              <div
                class="text-center color-info pdtb30 "
                v-if="controlCompanyList.length == 0"
              >
                暂无数据!
              </div>
            </div>
          </div>
        </el-collapse-transition>
      </div>
    </div>
  </div>
</template>

<script>
// fuse is a lightweight fuzzy-search module
// make search results more in line with expectations
import {colorList} from "@/formRules/system/OperationListRules";
import Fuse from "fuse.js";
import path from "path";
import { xqGethuitui } from "@/api";
export default {
  name: "companySearch",
  data() {
    return {
      search: "",
      options: [],
      searchPool: [],
      show: false,
      fuse: undefined,
      isTuihui: false,
      tuihuiList: [],
	  colorList:colorList()
    };
  },
  props: {
    companyList: {
      type: Array,
      default: () => {
        return [];
      }
    },
    nowCompanyId: {
      type: String,
      default: ""
    },
    companyId: {
      type: String,
      default: ""
    },
    eventId: {
      type: String,
      default: ""
    },
    type: {
      type: Number,
      default: ""
    }
  },
  computed: {
    routes() {
      return this.$store.getters.sidebarItems;
    },
    controlCompanyList() {
      if (!this.companyList.length) return [];
      let data = [];
      if (this.isTuihui) {
        data = this.tuihuiList.filter(
          i => i.companyName.indexOf(this.search) > -1
        );
      } else {
        data = this.companyList.filter(
          i => i.companyName.indexOf(this.search) > -1
        );
      }
      return data;
    }
  },
  watch: {
    show(value) {
      if (value) {
        document.body.addEventListener("click", this.close);
      } else {
        document.body.removeEventListener("click", this.close);
      }
    }
  },
  mounted() {
    this.getTuihuiList();
    // this.searchPool = this.generateRoutes(this.routes);
  },
  methods: {
    async getTuihuiList() {
      let params = {
        companyId: this.companyId,
        eventId: this.eventId,
        type: this.type
      };
      let { data } = await xqGethuitui(params);
      this.tuihuiList = data;
    },
    setCompanyId(id) {
      this.$emit("setCompanyId", id);
    },
    getbgColor(num) {
      return this.colorList[num].color;
    },
    click() {
      this.show = !this.show;
      if (this.show) {
        this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus();
      }
    },
    // close() {
    //   this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur();
    //   this.options = [];
    //   this.show = false;
    // },
    initFuse(list) {
      this.fuse = new Fuse(list, {
        shouldSort: true,
        threshold: 0.4,
        location: 0,
        distance: 100,
        maxPatternLength: 32,
        minMatchCharLength: 1,
        keys: [
          {
            name: "title",
            weight: 0.7
          },
          {
            name: "path",
            weight: 0.3
          }
        ]
      });
    },
    // Filter out the routes that can be displayed in the sidebar
    // And generate the internationalized title
    generateRoutes(routes, basePath = "/", prefixTitle = []) {
      let res = [];

      for (const router of routes) {
        // skip hidden router
        if (router.hidden) {
          continue;
        }

        const data = {
          path: path.resolve(basePath, router.path),
          title: [...prefixTitle]
        };

        if (router.meta && router.meta.title) {
          data.title = [...data.title, router.meta.title];

          if (router.redirect !== "noRedirect") {
            // only push the routes with title
            // special case: need to exclude parent router without redirect
            res.push(data);
          }
        }

        // recursive child routes
        if (router.children) {
          const tempRoutes = this.generateRoutes(
            router.children,
            data.path,
            data.title
          );
          if (tempRoutes.length >= 1) {
            res = [...res, ...tempRoutes];
          }
        }
      }
      return res;
    },
    querySearch(query) {
      if (query !== "") {
        console.log("this.fuse");
        console.log(this.fuse);
        this.options = this.fuse.search(query);
        console.log("this.options");
        console.log(this.options);
      } else {
        this.options = [];
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.header-search {
  .search-icon {
    cursor: pointer;
    font-size: 18px;
    vertical-align: middle;
  }

  .header-search-select {
    font-size: 18px;
    transition: width 0.2s;
    width: 0;
    // overflow: hidden;
    background: transparent;
    border-radius: 0;
    display: inline-block;
    vertical-align: middle;

    ::v-deep .el-input__inner {
      border-radius: 0;
      border: 0;
      padding-left: 0;
      padding-right: 0;
      box-shadow: none !important;
      border-bottom: 1px solid #d9d9d9;
      vertical-align: middle;
    }
  }

  &.show {
    .header-search-select {
      width: 210px;
      margin-left: 10px;
    }
  }
}
</style>
